package com.dingreading.cloud.web.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.TextUtils;
import com.dingreading.cloud.common.util.aliyun.AliyunMessageUtils;
import com.dingreading.cloud.web.entity.NjTicketSeller;
import com.dingreading.cloud.web.service.NjTicketSellerService;
import com.dingreading.cloud.web.util.QRCodeGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@RefreshScope
@RestController
@RequestMapping(value = "/front/ticketSeller")
@Api(tags = "ACEC销售员管理")
public class TicketSellerController {

    @Value("${aliYun.sms.accessKeyId}")
    private String accessKeyId;
    @Value("${aliYun.sms.accessKeySecret}")
    private String accessKeySecret;

    @Resource
    private NjTicketSellerService ticketSellerService;


    @ApiOperation(value = "创建销售员工二维码")
    @PostMapping(value = "/newSellers", produces = "application/json; charset=UTF-8")
    public R<Object> newSellers(
            @ApiParam(value = "公司名称") @RequestParam(value = "corpName") String corpName,
            @ApiParam(value = "公司标识（纯2位数字）") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "创建数量") @RequestParam(value = "num") Integer num
    ) {
        if (StringUtils.isBlank(corpName))
            return R.fail("公司名称不能为空");
        if (StringUtils.isBlank(corpUid))
            return R.fail("公司标识不能为空");
        if (num == null || num <= 0)
            return R.fail("创建数量必须大于0");

        int maxSellerCode = ticketSellerService.getMaxSellerCode(corpUid);
        // 生成销售员工码
        List<String> sellerCodeList = createSellerCode(maxSellerCode, num);
        if (CollectionUtils.isEmpty(sellerCodeList))
            return R.fail("销售员工码生成失败");

        String baseUrl = "https://acec.dingreading.com/?r=ticket/customer/tickethome";
        String basePath = "C:\\Users\\Administrator\\Desktop\\code\\" + corpUid + "\\" + DateUtil.getNowDateminStr() + "\\";
        File directory = new File(basePath);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        for (String code : sellerCodeList) {
            NjTicketSeller seller = new NjTicketSeller();
            seller.setCorpUid(corpUid);
            seller.setCorpName(corpName);
            seller.setSellerCode(code);
            ticketSellerService.saveOrUpdate(seller);

            String from = corpUid + code;
            String text = baseUrl + "&from=" + Integer.valueOf(from);
            String path = basePath + from + ".png";
            QRCodeGenerator.generateQRCodeImage(text, 300, 300, path);
        }

        return R.ok();
    }

    @ApiOperation(value = "获取销售员工信息")
    @PostMapping(value = "/getSeller", produces = "application/json; charset=UTF-8")
    public R<Object> getSeller(
            @ApiParam(value = "公司标识") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "销售人员编号") @RequestParam(value = "sellerCode") String sellerCode
    ) {
        if (StringUtils.isBlank(corpUid))
            return R.fail("公司标识不能为空");
        if (StringUtils.isBlank(sellerCode))
            return R.fail("销售人员标识不能为空");

        NjTicketSeller date = ticketSellerService.getSeller(corpUid, sellerCode);

        return R.ok(date);
    }

    @ApiOperation(value = "更新销售员工的姓名和手机号")
    @PostMapping(value = "/updateSellerInfo", produces = "application/json; charset=UTF-8")
    public R<Object> updateSellerInfo(
            @ApiParam(value = "公司标识") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "销售人员标识") @RequestParam(value = "sellerCode") String sellerCode,
            @ApiParam(value = "销售人员姓名") @RequestParam(value = "sellerName") String sellerName,
            @ApiParam(value = "销售人员手机") @RequestParam(value = "sellerPhone") String sellerPhone
    ) {
        if (StringUtils.isBlank(corpUid))
            return R.fail("公司标识不能为空");
        if (StringUtils.isBlank(sellerCode))
            return R.fail("销售人员标识不能为空");
        if (StringUtils.isBlank(sellerName))
            return R.fail("销售人员姓名不能为空");
        if (StringUtils.isBlank(sellerPhone))
            return R.fail("销售人员手机不能为空");

        boolean b = ticketSellerService.updateInfo(corpUid, sellerCode, sellerName, sellerPhone);
        if (!b)
            return R.fail("更新失败");

        return R.ok();
    }

    /**
     * 生成销售员工码
     */
    private List<String> createSellerCode(int maxSellerCode, Integer num) {
        int sellerCode = 0;
        if (maxSellerCode > 0)
            sellerCode = maxSellerCode;

        List<String> codeList = new ArrayList<>();
        for (int i = 1; i <= num; i++) {
            sellerCode += 1;

            // 数字最后一位是4则跳过
            sellerCode = TextUtils.dealLastFour(sellerCode);
            String nameSizeStr = String.valueOf(sellerCode);
            if (nameSizeStr.length() == 1) {
                nameSizeStr = "00" + nameSizeStr;
            } else if (nameSizeStr.length() == 2) {
                nameSizeStr = "0" + nameSizeStr;
            }
            codeList.add(nameSizeStr);
        }

        return codeList;
    }

    @ApiOperation(value = "测试-发送验证码")
    @PostMapping(value = "/sendMsg", produces = "application/json; charset=UTF-8")
    public R<Object> sendMsg(
            @ApiParam(value = "手机号") @RequestParam(value = "phone") String phone,
            @ApiParam(value = "验证码") @RequestParam(value = "code") String code
    ) throws Exception {
        String s = AliyunMessageUtils.acecSendVerifyCode(accessKeyId, accessKeySecret, phone, code);

        return R.ok(s);
    }

}
